package runnable_functions;

import gui.Main;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;


import details.*;



/**
 * @author Tami
 *
 */

public class DELETEfunctions extends RunnableClasses{

	
	
	public DELETEfunctions(){}
	
	/***********CLASSES********************/
	
	
		public class Delete implements Runnable{

			private String identifier;
			private ReferedObject refObj;
			private String userName;
			private CountDownLatch startSignal;
			
			public Delete(String identifier,ReferedObject refObj,String username,CountDownLatch startSignal){
				this.identifier = identifier;
				this.refObj = refObj;
				this.userName = username;
				this.startSignal = startSignal;
			}
			
			

			@Override
			public void run() {
				try{
					
					if(checkIfAdmin(userName)){
						Main.db.openConnection();
						if(refObj == ReferedObject.CD){
							Main.db.deleteCD(identifier);
							
							
						}
						else if(refObj == ReferedObject.ARTIST){
							Main.db.deleteArtist(Integer.parseInt(identifier));
							
							
						}
						else if(refObj == ReferedObject.GENRE){
							Main.db.deleteGenre(identifier);
							
							
						}
						else if(refObj == ReferedObject.LYRIC){
							Main.db.deleteLyrics(Integer.parseInt(identifier));
							
							
						}
						else if(refObj == ReferedObject.SONG){
							Main.db.deleteSong(Integer.parseInt(identifier));
							
							
						}
						else if(refObj == ReferedObject.TRACK){
							Main.db.deleteTrack(identifier);
							
							
						}
						Main.guiInt.notifySuccess();
					}
					else {
						Main.guiInt.notifyGeneralError("you dont have authority to delete");
					}
				}
				catch(SQLException e){
					Main.guiInt.notifyDBFailure(e.toString());
				}
				finally{
					Main.db.closeConnection();
					startSignal.countDown();
					
				}
			}
		}
		
		public class DeleteEmptyCds implements Runnable{
			
			private CountDownLatch startSignal;
			public DeleteEmptyCds(CountDownLatch startSignal){
				this.startSignal = startSignal;
			}

			@Override
			public void run() {
				Main.db.openConnection();
				try {
					Main.db.deleteEmptyCds();
				} catch (SQLException e) {
					Main.guiInt.notifyDBFailure(e.getMessage());
				}
				finally{
					Main.db.closeConnection();
					startSignal.countDown();
				}
				
				
			}
			
		}
		
		
		public class DeleteComment implements Runnable{

			private Comment comment;
			private User user;
			private int song_id;
			private CountDownLatch startSignal;
			private ArrayList<Comment> updatedComments =null;
			public DeleteComment(Comment comment,User user,int song_id,CountDownLatch startSignal){
				this.comment = comment;
				this.user = user;
				this.song_id = song_id;
				this.startSignal = startSignal;
			}
			
			

			@Override
			public void run() {
				try{
					System.out.println("the input parameters are "+comment.getComment() +" "+user.getUserName() +" "+song_id);
					System.out.println("we aretrying to delete the comment");
					if(checkIfAdmin(user.getUserName()) ||user.getUserName().equals(comment.getUser().getUserName())){
						Main.db.openConnection();
						System.out.println("we call db delete comment function");
						Main.db.deleteSongComment(comment.getIdNum());
						System.out.println("the comment was deleted");
						updatedComments= Main.db.getSongComments(song_id);
						System.out.println("we have updated the comment list");
						
						Main.guiInt.refreshSongComments(updatedComments);
						
							
						}
					else {
						Main.guiInt.notifyGeneralError("you dont have authority to delete this comment");
					}
				}
				catch(SQLException e){
					Main.guiInt.notifyDBFailure(e.toString());
				}
				finally{
					Main.db.closeConnection();
					startSignal.countDown();
				}
			}
		}
		
		public class Unlike implements Runnable{

			private Target target;
			private String targetId;
			private User user;
			private int likes;
			private CountDownLatch startSignal;
			
			public Unlike(User user,Target target,String targetId,CountDownLatch startSignal){
				this.target = target;
				this.targetId = targetId;
				this.user = user;
				this.startSignal = startSignal;
			}
			
			

			@Override
			public void run() {
				try{
					System.out.println("we are in delete like trying to unlike");
					Main.db.openConnection();
					System.out.println("we pass Like.UNLIKE to db");
					Main.db.like_UnLike(targetId, target, Like.UNLIKE, user.getIdNum());
					System.out.println("we return from db");
					likes = Main.db.getNumOfLikes(targetId, target);
					Main.guiInt.refreshUserLike(details.Like.UNLIKE);
					switch(target){
					case CD:
						Main.guiInt.refreshCdLikes(likes);
						break;
					case SONG:
						Main.guiInt.refreshSongLikes(likes);
						break;
					case ARTIST:
						Main.guiInt.refreshArtistLikes(likes);
						break;
					default:
						break;
					}
						
				}
				catch(SQLException e){
					Main.guiInt.notifyDBFailure(e.toString());
				}
				finally{
					Main.db.closeConnection();
					startSignal.countDown();
				}
			}
		}
		
		
		
		
	
		
}
